package com.doge.calcalbum.business.vip

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.doge.calcalbum.App
import com.doge.calcalbum.business.account.AccountHelper.self
import com.doge.calcalbum.db.model.Album
import com.doge.calcalbum.utils.*
import kotlin.math.max

class AppVipViewModel : ViewModel() {
    val limitVideoCount = MutableLiveData<Int>()
    val limitPictureCount = MutableLiveData<Int>()

    fun refreshLimitCount() {
        require(isMainThread())
        if (self.isVip) {
            limitVideoCount.value = Int.MAX_VALUE
            limitPictureCount.value = Int.MAX_VALUE
        } else runOnIO {
            limitVideoCount.postValue(
                max(0, freeLimitCount - db.albumDao().totalCount(Album.TYPE_VIDEO))
            )
            limitPictureCount.postValue(
                max(0, freeLimitCount - db.albumDao().totalCount(Album.TYPE_PICTURE))
            )
        }
    }

    companion object {
        fun get(): AppVipViewModel {
            return (appContext as App).getAppViewModelProvider().get(AppVipViewModel::class.java)
        }
    }
}